package com.zhangyang.esper.pattern;

import com.alibaba.fastjson.JSONObject;
import com.espertech.esper.client.*;
import com.espertech.esper.client.scopetest.SupportUpdateListener;
import com.espertech.esper.client.time.CurrentTimeEvent;
import com.espertech.esper.client.util.DateTime;
import com.espertech.esper.common.internal.support.SupportBean;
import com.espertech.esper.metrics.instrumentation.InstrumentationHelper;
import com.zhangyang.esper.support.bean.SupportBean_S5;
import com.zhangyang.esper.support.bean.SupportCommonBean;
import com.zhangyang.esper.support.bean.SupportMarketDataBean;
import junit.framework.TestCase;

import java.util.List;

/**
 * @author Dell
 * @Title:
 * @Description: 请写注释类
 * @date 2022/9/16
 */
public class GlobalOperator extends TestCase {

    public EPServiceProvider epService;

    public void testGlobalOperator() throws InterruptedException {

        Configuration config = new Configuration();
        config.getEngineDefaults().getLogging().setEnableQueryPlan(true);
        config.getEngineDefaults().getThreading().setInternalTimerEnabled(true);
//        config.addEventType("A", SupportBean.class);
        config.addEventType("C", SupportBean_S5.class);
        config.addEventType("B", SupportMarketDataBean.class);
        config.addEventType("S", SupportCommonBean.class);
        epService = EPServiceProviderManager.getDefaultProvider(config);
        epService.initialize();
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(0));
        if (InstrumentationHelper.ENABLED) { InstrumentationHelper.startTest(epService, this.getClass(), getName());}
//        String a = " select *, count(1) as countNum from  A ";
        String b = "insert into newB select  * from  B  having count(1) >1 ";
        epService.getEPAdministrator().createEPL(b);
        String c = " insert into newC select *  from  C having count(1)>0  ";
        epService.getEPAdministrator().createEPL(c);
//        epService.getEPAdministrator().createEPL(a);
//        epService.getEPAdministrator().createEPL(b);
//        epService.getEPAdministrator().createEPL(c);
//        String s = "  select * from  S ";vand  timer:interval(1 second)
//select * from  pattern [ every [1] A=$$O(logoId='f6426215-e330-4e38-add6-15389c1a2c9a') null every [1] B=$$O(logoId='34cb8d5f-a13a-4a0f-956f-b425d15eac4b') null every [1] C=$$O(logoId='d2b6f630-4fcc-467b-b0ea-38f8ecc81ef3')  and  timer:interval(10ms)  ]
        String stmtText = "select  a ,c from  pattern[ every(   [1]c = newC   or    [1]a = newB  )   ] ";
        EPStatement statement = epService.getEPAdministrator().createEPL(stmtText);
        SupportUpdateListener listener = new SupportUpdateListener();
        statement.addListener(listener);
        SupportMarketDataBean supportMarketDataBeanA = new SupportMarketDataBean("E6", "M1", 0);
        supportMarketDataBeanA.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:01.550"));
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:01.550")));
        epService.getEPRuntime().sendEvent(supportMarketDataBeanA);
//        Thread.sleep(5000);
        SupportMarketDataBean supportMarketDataBean = new SupportMarketDataBean("E5", "M1", 1);
        supportMarketDataBean.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:12.550"));
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:12.550")));
        epService.getEPRuntime().sendEvent(supportMarketDataBean);
//        assertTrue(listener.isInvoked());
        SupportMarketDataBean supportMarketDataBean1 = new SupportMarketDataBean("E4", "M1", 1);
        supportMarketDataBean1.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:12.550"));
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:14.550")));
        epService.getEPRuntime().sendEvent(supportMarketDataBean1);

        SupportBean_S5 supportBean_s5 = new SupportBean_S5(1, "1");
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:15.500")));
        supportBean_s5.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:15.500"));
        epService.getEPRuntime().sendEvent(supportBean_s5);
//        Thread.sleep(5000);
        SupportBean_S5 supportBean_s51 = new SupportBean_S5(2, "2");
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:22.500")));
        supportBean_s51.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:22.500"));
        epService.getEPRuntime().sendEvent(supportBean_s51);
        List<EventBean[]> newDataList = listener.getNewDataList();
        for (EventBean[] newEvent : newDataList) {
            for (EventBean eventBean : newEvent) {
                System.err.println(JSONObject.toJSONString(eventBean));
            }

        }

        assertTrue(listener.isInvoked());
    }

    public void testGlobalOperatorMatch() throws InterruptedException {

        Configuration config = new Configuration();
        config.getEngineDefaults().getLogging().setEnableQueryPlan(true);
        config.getEngineDefaults().getThreading().setInternalTimerEnabled(true);
//        config.addEventType("A", SupportBean.class);
        config.addEventType("C", SupportBean_S5.class);

        epService = EPServiceProviderManager.getDefaultProvider(config);
        epService.initialize();
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(0));
        if (InstrumentationHelper.ENABLED) { InstrumentationHelper.startTest(epService, this.getClass(), getName());}
//        String a = " create context CtxA partition by id from C ";


        String c = "@Audit('初始值')  select  * from  C.win:ext_timed(longPrimitive, 10 sec)  group by id";
        epService.getEPAdministrator().createEPL(c);
//        epService.getEPAdministrator().createEPL(a);
//        epService.getEPAdministrator().createEPL(b);
//        epService.getEPAdministrator().createEPL(c);
//        String s = "  select * from  S ";vand  timer:interval(1 second)
//select * from  pattern [ every [1] A=$$O(logoId='f6426215-e330-4e38-add6-15389c1a2c9a') null every [1] B=$$O(logoId='34cb8d5f-a13a-4a0f-956f-b425d15eac4b') null every [1] C=$$O(logoId='d2b6f630-4fcc-467b-b0ea-38f8ecc81ef3')  and  timer:interval(10ms)  ]
//        String stmtText = "@Audit('内容')  select  * from C  group by id, pattern[ every(   [2]c = C(P50=1) AND  [2]b = C(P50=2)  )   ]  ";
        String stmtText = "@Audit('内容')  select  * from  pattern[ every(   [2]c = C(P50=1) AND  [2]b = C(P50=2)  )   ]  ";
        EPStatement statement = epService.getEPAdministrator().createEPL(stmtText);
        SupportUpdateListener listener = new SupportUpdateListener();
        statement.addListener(listener);
//        SupportMarketDataBean supportMarketDataBeanA = new SupportMarketDataBean("E6", "M1", 0);
//        supportMarketDataBeanA.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:01.550"));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:01.550")));
//        epService.getEPRuntime().sendEvent(supportMarketDataBeanA);
////        Thread.sleep(5000);
//        SupportMarketDataBean supportMarketDataBean = new SupportMarketDataBean("E5", "M1", 1);
//        supportMarketDataBean.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:12.550"));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:12.550")));
//        epService.getEPRuntime().sendEvent(supportMarketDataBean);
////        assertTrue(listener.isInvoked());
//        SupportMarketDataBean supportMarketDataBean1 = new SupportMarketDataBean("E4", "M1", 1);
//        supportMarketDataBean1.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:12.550"));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:14.550")));
//        epService.getEPRuntime().sendEvent(supportMarketDataBean1);

        SupportBean_S5 supportBean_s5 = new SupportBean_S5(1, "1");
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:15.500")));
        supportBean_s5.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:15.500"));
        epService.getEPRuntime().sendEvent(supportBean_s5);
//        Thread.sleep(5000);
        SupportBean_S5 supportBean_s51 = new SupportBean_S5(2, "2");
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:22.500")));
        supportBean_s51.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:22.500"));
        epService.getEPRuntime().sendEvent(supportBean_s51);

        SupportBean_S5 supportBean_s52 = new SupportBean_S5(2, "2");
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:23.500")));
        supportBean_s52.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:23.500"));
        epService.getEPRuntime().sendEvent(supportBean_s52);

        SupportBean_S5 supportBean_s53 = new SupportBean_S5(2, "1");
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:24.500")));
        supportBean_s53.setLongPrimitive(DateTime.parseDefaultMSec("2002-02-01T9:00:24.500"));
        epService.getEPRuntime().sendEvent(supportBean_s53);
        List<EventBean[]> newDataList = listener.getNewDataList();
        for (EventBean[] newEvent : newDataList) {
            for (EventBean eventBean : newEvent) {
                System.err.println(JSONObject.toJSONString(eventBean));
            }

        }

        assertTrue(listener.isInvoked());
    }



}
